from lxml import etree

from framework.common import util
from framework.core.crawler_result import Result
from framework.core.crawler_task import Task


class KeywordsParser(object):
    @staticmethod
    def parse(response, current_task):
        content = response.content
        tree = etree.HTML(content)
        results = list()
        asin_list = tree.xpath('//div[@data-asin]/@data-asin')
        keywords_list = list()
        for asin in asin_list:
            keywords_dict = dict()
            if asin is not None and str(asin) != "":
                keywords_dict['asin'] = asin
                keywords_list.append(keywords_dict)

        current_products_result = Result.from_task(task=current_task, products=keywords_list)
        current_products_result.metadata["tr"] = "keywords"
        results.append(current_products_result)

        new_task_list = Task.create_tasks(util.fstring(""), parser='default',
                                          parent_result=current_products_result,
                                          metadata=current_task.copy_metadata(
                                              {"tp": "last_page"}))  # 如果是最后一页，则next_page_url为空，所以tasks也为空

        review_result = Result.from_task(task=current_task, reviews=keywords_list)
        return review_result, new_task_list


    @staticmethod
    def parse_keywords(response, current_task):
        parent_result = current_task.parent_result
        content = response.content
        tree = etree.HTML(content)
        keywords_list = list()
        results = list()
        div_keywords_list = tree.xpath("//div[@class='s-main-slot s-result-list s-search-results sg-row']/div")

        index = 0
        for div_keywords in div_keywords_list:
            keywords_dict = dict()
            product_title = div_keywords.xpath("descendant::*/div[@class='a-section a-spacing-none']/h2/a/span/text()")

            # filter the empty row
            if product_title is None or str(product_title) is "" or len(product_title) == 0:
                continue

            keywords_dict["product_title"] = util.fstring(product_title)

            img_url = div_keywords.xpath("descendant::*/div[@class='a-section a-spacing-none']/span/a/div/img/@src")
            keywords_dict["img_url"] = util.fstring(img_url)

            img_alt = div_keywords.xpath("descendant::*/div[@class='a-section a-spacing-none']/span/a/div/img/@alt")
            keywords_dict["img_alt"] = util.fstring(img_alt)

            spans = div_keywords.xpath("descendant::*/div[@class='a-section a-spacing-none a-spacing-top-micro']/div/span")
            keywords_dict["stars"] = util.fstring(spans[1].xpath('@aria-label'))

            price = div_keywords.xpath("descendant::*/div[@class='a-row a-size-base a-color-base']/a/span/span/text()")
            keywords_dict["price"] = util.fstring(price)

            # todo asin

            keywords_list.append(keywords_dict)
            index = index + 1
            print(index)

        current_products_result = Result.from_task(task=current_task, products=keywords_list)
        current_products_result.metadata["tr"] = "keywords"

        next_page_url = tree.xpath("//ul[@class='a-pagination']/li[@class='a-last']/a/@href")
        next_page_url = util.fstring(next_page_url)

        next_or_last_page = "next_page" if len(util.fstring(next_page_url)) > 0 else "last_page"
        current_products_result.metadata["tp"] = next_or_last_page
        results.append(current_products_result)

        new_task_list = Task.create_tasks(util.fstring(next_page_url), parser='default',
                                           parent_result=current_products_result,
                                           metadata=current_task.copy_metadata({"tp": next_or_last_page}))  # 如果是最后一页，则next_page_url为空，所以tasks也为空

        review_result = Result.from_task(task=current_task, reviews=keywords_list)
        return review_result, new_task_list


if __name__ == "__main__":
    from framework.mock import MockResponse, MockTask
    parser = KeywordsParser()
    sample_local_html = "./samples/keywords_fan.html"
    response = MockResponse(sample_local_html)
    parser.parse(response, MockTask(None))
